package com.kfm.am;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestDBUtils2 {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3306/kfm";
        // 无参构造创建 QueryRunner 对象，把与数据库建立连接需要调用者手动传入 update query
        QueryRunner queryRunner = new QueryRunner();
        // 没有提供数据源
        // 加载驱动 加载失败 返回false 可以避免出现 ClassNotFoundException
        if (DbUtils.loadDriver("com.mysql.cj.jdbc.Driver")) {
//            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, "root", "");
//        String update = "update account set balance = balance + ? where id = ?";
//        int update1 = queryRunner.update(conn, update, 5000, 1);
//        System.out.println(update1);
            String sql = "select * from users where name = ? and password = ?";
            // new User().getClass()
            User ls = queryRunner.query(conn, sql, new BeanHandler<>(User.class), "ls", "123456");
            System.out.println(ls);
//        DbUtils.close(conn);
            // 传递的对象为null时不会触发关闭操作 还可以有效避免抛出 SQLException 异常
            DbUtils.closeQuietly(conn);
        } else {
            System.out.println("加载驱动失败");
        }
//


    }
}
